package helpers;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.channels.Channels;
import java.util.Collections;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.files.AppEngineFile;
import com.google.appengine.api.files.FileReadChannel;
import com.google.appengine.api.files.FileService;
import com.google.appengine.api.files.FileServiceFactory;
import com.google.appengine.api.files.FileWriteChannel;

import entities.Dns;

public class FileHelper {
	
	public static Dns getDnsByFileName(String filename) throws IOException
	{
		Cache cache=null;
    	try {
		    cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
		} catch (CacheException e1) {
		   e1.printStackTrace();
		}
		
		
		if(cache!=null&&cache.containsKey(filename))
		{
			return (Dns) cache.get (filename)	;
		}
		else
		{
			
			String key = DataStoreHelper.getKeyFileName(filename);
			if(key==null)
			{
				return null;
			}
			FileService fileService = FileServiceFactory.getFileService();
			
			AppEngineFile file=fileService.getBlobFile(new BlobKey(key));
			FileReadChannel readChannel = fileService.openReadChannel(file, false);
			ObjectInputStream ooi = new ObjectInputStream(Channels.newInputStream(readChannel));
			Dns dns=null;
			try {
				dns = (Dns)ooi.readObject();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(cache!=null)
			{
				cache.put(filename, dns);
			}
			return dns;
		}
		
	}
	public static Dns getDnsByKey(String key) throws ClassNotFoundException, IOException
	{
		Cache cache=null;
    	try {
		    cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
		} catch (CacheException e1) {
		   e1.printStackTrace();
		}
		
		
		
		if(cache!=null&&cache.containsKey(key))
		{
			return (Dns) cache.get(key)	;
		}
		else
		{
			
			
			FileService fileService = FileServiceFactory.getFileService();
			
			AppEngineFile file=fileService.getBlobFile(new BlobKey(key));
			FileReadChannel readChannel = fileService.openReadChannel(file, false);
			ObjectInputStream ooi = new ObjectInputStream(Channels.newInputStream(readChannel));
			Dns dns = (Dns) ooi.readObject();
			if(cache!=null)
			{
				cache.put(key, dns);
			}
			return dns;
		}
		
	}
	
	public static String savePage(Dns dns) {
		FileService fileService = FileServiceFactory.getFileService();
		
		AppEngineFile file;
		try {
			file = fileService.createNewBlobFile("text/html", dns.getDomain());
			
			
		FileWriteChannel writeChannel = fileService
				.openWriteChannel(file, true);
	

		ObjectOutputStream oos = new ObjectOutputStream(
				Channels.newOutputStream(writeChannel));
		oos.writeObject(dns);
		oos.flush();
		oos.close();
		// Now finalize
		writeChannel.closeFinally();
		
		Cache cache=null;
    	try {
		    cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
		} catch (CacheException e1) {
		   e1.printStackTrace();
		}
		String cachePrefix="datastore_";
		String key = fileService.getBlobKey(file).getKeyString();
		if(cache!=null)
		{
			cache.put(dns.getDomain(), dns);
			cache.put(cachePrefix+dns.getDomain(), key);
		}
		return key;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
}
